#include "key_manage.h"

static key_handle_t _key_slist_head = NULL;  // 按键管理单链表头结点

/**
 * @brief   初始化按键对象
 * @param   key             按键对象句柄
 * @param   gpio_pin_read   获取按键电平函数指针
 * @param   active_level    按键按下有效电平
 * @return  0: succeed -1: failed
 */
int8_t key_init(struct key_handle *key, uint8_t (*gpio_pin_read)(void), uint8_t active_level)
{
    if (key == NULL)
        return -1;
    memset(key, 0, sizeof(struct key_handle));
    key->event = None_Press;
    key->active_level = active_level;
    key->pin_read = gpio_pin_read;
    key->key_level = key->pin_read();

    return 0;
}

/**
 * @brief   注册按键:将按键对象插入到按键管理链表中
 * @param   key 按键对象句柄
 * @return  0: succeed -1: failed
 */
int8_t key_handle_register(struct key_handle *key)
{
    struct key_handle *key_slist_node = _key_slist_head;  // 获取头指针的地址 (无头结点单链表)

    if (key == NULL)
        return -1;

    // 尾插(不带头结点的单链表, 头指针需做特殊判断)
    if (_key_slist_head == NULL) // 头指针为空==表空
    {
        _key_slist_head = key;
        key->next = NULL;
        return 0;
    }

    while(key_slist_node)
    {
        if (key_slist_node == key) return -1;       // 重复注册
        if(key_slist_node->next == NULL) break;     // 已经遍历到最后一个节点,必须在此跳出循环, 否则key_slist_node==NULL
        key_slist_node = key_slist_node->next;
    }
    key_slist_node->next = key;  
    key->next = NULL; 

    return 0;
}

/**
 * @brief   脱离按键:将按键对象从按键管理链表中脱离
 * @param   key 按键对象句柄
 * @return  0: succeed -1: failed
 */
int8_t key_handle_detach(struct key_handle *key)
{
    // 解1级引用指向指针变量, 解2级引用指向指针变量所指向的变量 
    struct key_handle **key_slist_node = &_key_slist_head;  // 指向头指针, 直接操作原指针变量(不然最后无法修改头指针)
    struct key_handle *node_temp;
    if (key == NULL || _key_slist_head == NULL)
        return -1;

    while(*key_slist_node && *key_slist_node != key)
    {
        node_temp = *key_slist_node;
        if((*key_slist_node)->next == NULL) break;
        key_slist_node = &node_temp->next;     // 不能直接解1级引用赋值，会破坏原链表
    }

    if (*key_slist_node != key)
        return -1;
    *key_slist_node = (*key_slist_node)->next;

    return 0;
}

/**
 * @brief   注册按键事件触发回调函数
 * @param   key             按键对象句柄
 * @param   event           触发事件类型
 * @param   event_callback  事件回调函数
 * @return  0: succeed -1: failed
 */
int8_t key_event_callback_register(struct key_handle *key, uint8_t event, void (* event_callback)(key_handle_t key))
{
    if (key == NULL || event >= Event_Sum)
        return -1;
    key->event_callback[event] = event_callback;

    return 0;
}

/**
 * @brief   处理所有按键对象的状态机
 * @param   key 按键对象句柄
 * @return  None
 */
static void key_handler(struct key_handle *key)
{
    uint8_t key_level_temp = key->pin_read();

    if(key->state != Init_None_State) key->tick++;

    /* 按键消抖(按键状态发生变化保持DEBOUNCE_TICK时间片开始保存按键引脚电平) */
    if(key_level_temp != key->key_level)
    {
        if(++(key->debounce_tick) >= Double_Click)
        {
            key->key_level = key_level_temp;
            key->debounce_tick = 0;
        }
    }
    else
    {
        key->debounce_tick = 0;
    }
        
    /* 按键状态机 */
    switch (key->state)
    {
    case Init_None_State:
        /* 初始态-> 初始按下态  Press_Down */
        if(key->key_level == key->active_level)   
        {
            key->event = (uint8_t)Press_Down;
            __KEY_EVENT_CALL(Press_Down);
            key->tick  = 0;
            key->repeat_cnt = 1;
            key->state  = Init_Press_State;
        }
        else
        {
            key->event = (uint8_t)None_Press;
        }    
        break;

    case Init_Press_State:
        /* 第一次按下松开：初始按下态->连击检查态  Press_Up */
        if(key->key_level != key->active_level)
        {
            key->event = (uint8_t)Press_Up;
            __KEY_EVENT_CALL(Press_Up);
            key->tick = 0;
            key->state = Press_Check_State;

        }
        /* 第一次按下后长按(>LONG_PRESS_START_TICK)：初始按下态->长按态  Long_Press_Start */
        else if(key->tick > LONG_PRESS_START_TICK)
        { 
            key->event = (uint8_t)Long_Press_Start;
            __KEY_EVENT_CALL(Long_Press_Start);
            key->state = Long_Press_State;
        }
        break;

    case Press_Check_State:
        /* 松开后再次按下：连击检查态->连击态 Press_Down & Short_Press_Repeat */
        if(key->key_level == key->active_level)
        {
            key->event = (uint8_t)Press_Down;
            __KEY_EVENT_CALL(Press_Down);
            key->repeat_cnt++;
            __KEY_EVENT_CALL(Short_Press_Repeat);
            key->tick = 0;
            key->state = Continuous_Press_State;
        } 
        /* 松开后再次没有按下(>SHORT_PRESS_START_TICK)：连击检查态->初始态 repeat_cnt=1: Singe_Click; repeat_cnt=2: Double_Click */
        else if(key->tick > SHORT_PRESS_START_TICK)
        {                
            if(key->repeat_cnt == 1)
            {
                key->event = (uint8_t)Singe_Click;
                __KEY_EVENT_CALL(Singe_Click);
            }
            /* 连击态松开后会返回此条件下触发  todo: <可以做n连击判断> */
            else if(key->repeat_cnt == 2)
            {
                key->event = (uint8_t)Double_Click;
                __KEY_EVENT_CALL(Double_Click);
            }
            key->state = Init_None_State;
        }
        break;

    case Continuous_Press_State:
        /* 连击后松开：连击态->连击检查态(< SHORT_PRESS_START_TICK)) : 连击态->初始态(>= SHORT_PRESS_START_TICK) */
        if(key->key_level != key->active_level)
        {
            key->event = (uint8_t)Press_Up;
            __KEY_EVENT_CALL(Press_Up);

            if(key->tick < SHORT_PRESS_START_TICK)
            {
                key->tick = 0;
                key->state = Press_Check_State;
            }
            else
            {
                key->state = Init_None_State;
            }
        }
        /* 连击后长按(>SHORT_TICKS): 连击态 -> 初始态 */
        else if(key->tick > SHORT_PRESS_START_TICK)
        {
            key->state = Init_Press_State;  // 可以回到Init_None_State/Init_Press_State
        }
        break;

    case Long_Press_State:
        /* 长按保持  Long_Press_Hold */
        if(key->key_level == key->active_level)
        {
            key->event = (uint8_t)Long_Press_Hold;
            if (key->tick % LONG_HOLD_CYCLE_TICK == 0)     
            {
                __KEY_EVENT_CALL(Long_Press_Hold);
            }
        }
        /* 长按松开：长按态-> 初始态 */
        else
        {
            key->event = (uint8_t)Press_Up;
            __KEY_EVENT_CALL(Press_Up);

            key->state = Init_None_State;
        }
        break;
    }  
}

/**
 * @brief   每经过一个滴答周期调用一次按键处理函数(裸机放1ms中断, OS放线程或中断)
 * @param   None
 * @return  None
 */
void key_tick(void)
{
    struct key_handle *key_slist_node;
    static uint8_t tick_cnt = 0;

    if (++tick_cnt < CHECK_TICK)
        return;
    
    for (key_slist_node = _key_slist_head; key_slist_node != NULL; key_slist_node = key_slist_node->next)
    {
        key_handler(key_slist_node);
    }
    tick_cnt = 0;
}
